<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="oaid" prop="oaid">
        <el-input
          v-model="queryParams.oaid"
          placeholder="请输入oaid"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备id" prop="hardwareinfo">
        <el-input
          v-model="queryParams.hardwareinfo"
          placeholder="请输入设备id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:blacklist:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="blacklistList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="oaid" align="center" prop="oaid" />
      <el-table-column label="设备id" align="center" prop="hardwareinfo" />
      <el-table-column label="类型" align="center" prop="remarks" />
      <!-- <el-table-column label="备注" align="center" prop="remarks" /> -->
      <el-table-column label="厂商" align="center" prop="brand" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="安卓版本" align="center" prop="osversion" />
      <el-table-column label="ip" align="center" prop="ip" />
      <el-table-column label="创建时间" align="center" prop="createtime">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createtime) }}</span>
          </template>
        </el-table-column>
      <el-table-column label="过期时间" align="center" prop="expiretime">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.expiretime) }}</span>
          </template>
        </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:blacklist:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listBlacklist, getBlacklist, delBlacklist, addBlacklist, updateBlacklist, exportBlacklist } from "@/api/business/blacklist/blacklist";

export default {
  name: "Blacklist",
  data() {
    return {
      // 日期范围
      dateRange: [new Date(),new Date()],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 临时黑名单库表格数据
      blacklistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        oaid: null,
        hardwareinfo: null,
        blacklistType: null,
        remarks: null,
        brand: null,
        model: null,
        osversion: null,
        imei: null,
        imei2: null,
        androidid: null,
        ip: null,
        ipsegment: null,
        logcode: null,
        logtype: null,
        createtime: null,
        expiretime: null,
        lastmodifytime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        blacklistType: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatDate(date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    /** 查询临时黑名单库列表 */
    getList() {
      this.loading = true;
      let date = [];
      if(this.dateRange){
        date[0] = this.formatDate(this.dateRange[0]);
        date[1] = this.formatDate(this.dateRange[1]);
      }
      listBlacklist(this.addDateRange(this.queryParams, date)).then(response => {
        this.blacklistList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        temporaryRosterId: null,
        oaid: null,
        hardwareinfo: null,
        blacklistType: null,
        remarks: null,
        brand: null,
        model: null,
        osversion: null,
        imei: null,
        imei2: null,
        androidid: null,
        ip: null,
        ipsegment: null,
        logcode: null,
        logtype: null,
        createtime: null,
        expiretime: null,
        lastmodifytime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [new Date(),new Date()];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.temporaryRosterId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加临时黑名单库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const temporaryRosterId = row.temporaryRosterId || this.ids
      getBlacklist(temporaryRosterId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改临时黑名单库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.temporaryRosterId != null) {
            updateBlacklist(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBlacklist(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const temporaryRosterIds = row.temporaryRosterId || this.ids;
      this.$modal.confirm('是否确认删除临时黑名单库编号为"' + temporaryRosterIds + '"的数据项？').then(function() {
        return delBlacklist(temporaryRosterIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有临时黑名单库数据项？').then(() => {
        this.exportLoading = true;
        return exportBlacklist(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
